package com.ruoyi.quartz.task;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.order.OrderUtils;
import com.ruoyi.goods.domain.TGoodsInfo;
import com.ruoyi.goods.service.ITGoodsInfoService;
import com.ruoyi.statistic.domain.WMonthExitWarehouseStat;
import com.ruoyi.statistic.domain.WStatisticDimensionBigit;
import com.ruoyi.statistic.service.IWMonthExitWarehouseGoodsRelService;
import com.ruoyi.statistic.service.IWMonthExitWarehouseStatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.sql.Date;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 定时任务调度测试
 * 
 * @author wzy
 */
@Component("mxTask")
public class MxTask
{
    @Autowired
    private ITGoodsInfoService tGoodsInfoService;

    @Autowired
    private IWMonthExitWarehouseStatService wMonthExitWarehouseStatService;

    @Autowired
    private IWMonthExitWarehouseGoodsRelService wMonthExitWarehouseGoodsRelService;

    private static Logger logger = LoggerFactory.getLogger(MxTask.class);

    /**
     * 统计月出库量，无参，按出库客户，货物类型，维度统计
     */

    public void statisticMonthExitWarehouseTotal(String strExitWarehouseDate)
    {
        logger.debug("统计，月出库量，出库客户，货物类型，开始 ...");
        /**
         * 计算当前日期，前一月日期
         */
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate currentDate = LocalDate.now();
        if(StringUtils.isNotEmpty(strExitWarehouseDate)) {
            currentDate = LocalDate.parse(strExitWarehouseDate, formatter);
        }
        logger.debug("当前日期: " + currentDate);

        LocalDate lastMonthDate = currentDate.minusMonths(1);
        logger.debug("上月日期: " + lastMonthDate);
        LocalDate lastMonthFirstDay = lastMonthDate.with(TemporalAdjusters.firstDayOfMonth());
        logger.debug("上个月第一天：" + lastMonthFirstDay);
        LocalDate lastMonthLastDay = lastMonthDate.with(TemporalAdjusters.lastDayOfMonth());
        logger.debug("上个月最后一天：" + lastMonthLastDay);

        TGoodsInfo tGoodsInfo = new TGoodsInfo();

        Double exitWarehouseWeightTotal = 0.0, exitWarehouseTheoryWeightTotal = 0.0;

        WMonthExitWarehouseStat wMonthExitWarehouseStat = new WMonthExitWarehouseStat();

        String monthExitStatisticNumber;

        Long monthExitStatisticId = 0L;

        List<Long> goodsIds = new ArrayList<>();
        /**

        /**
         * 按出库日期，筛选出库客户，货物类型，列表
         */
        List<WStatisticDimensionBigit> statisticDimensionBigits = tGoodsInfoService.selectStatisticDimensionByExitWarehouseDate(Date.valueOf(lastMonthFirstDay), Date.valueOf(lastMonthLastDay));
        //logger.debug("statisticDimensionBigits: " + statisticDimensionBigits);
        //logger.debug("statisticDimensionBigits: " + (statisticDimensionBigits instanceof Map) );
        //logger.debug("statisticDimensionBigits: " + (statisticDimensionBigits instanceof List) );

        /**
         * 遍历合约号，货物
         */
        for(WStatisticDimensionBigit statisticDimensionBigit : statisticDimensionBigits)
        {

            /**
             * 按出库客户，货物类型。出库日期，查询货物
             */
            tGoodsInfo.setExitWarehouseCustomer(statisticDimensionBigit.getCustomer());
            tGoodsInfo.setGoodsType(statisticDimensionBigit.getGoodsType());
            //tGoodsInfo.setExitWarehouseTime(Date.valueOf(yesterday));
            //logger.debug("OOO " + "statisticDayExitWarehouseTotal = " + tGoodsInfo);

            List<TGoodsInfo> goods = tGoodsInfoService.selectTGoodsInfoListUsedByExitStatistic(tGoodsInfo, Date.valueOf(lastMonthFirstDay), Date.valueOf(lastMonthLastDay));
            /**
             * 合计出库货物重量，记录货物ID
             */
            for(TGoodsInfo good : goods)
            {
                exitWarehouseWeightTotal = exitWarehouseWeightTotal + good.getWeight();
                exitWarehouseTheoryWeightTotal = exitWarehouseTheoryWeightTotal + good.getTheoryWeight();
                goodsIds.add(good.getGoodsId());
            }

            /**
             * 保护，goodsIds，为空，直接退出，本次循环
             */
            if(goodsIds.isEmpty()) break;

            /**
             * 月出库量编号
             */
            monthExitStatisticNumber = OrderUtils.getOrderNumber("MX", "-");

            /**
             * 出库重量，四舍五入
             */
            exitWarehouseWeightTotal = Math.round(exitWarehouseWeightTotal * 1000.0) / 1000.0;
            exitWarehouseTheoryWeightTotal = Math.round(exitWarehouseTheoryWeightTotal * 1000.0) / 1000.0;

            /**
             * 月出库量，数据准备
             */
            wMonthExitWarehouseStat.setMonthExitStatisticNumber(monthExitStatisticNumber);
            wMonthExitWarehouseStat.setExitWarehouseDate(Date.valueOf(lastMonthDate));
            wMonthExitWarehouseStat.setExitWarehouseCustomer(statisticDimensionBigit.getCustomer());
            wMonthExitWarehouseStat.setGoodsType(statisticDimensionBigit.getGoodsType());
            wMonthExitWarehouseStat.setWeightMode(statisticDimensionBigit.getWeightMode());
            wMonthExitWarehouseStat.setMonthExitWarehouseWeight(exitWarehouseWeightTotal);
            wMonthExitWarehouseStat.setMonthExitWarehouseTheoryWeight(exitWarehouseTheoryWeightTotal);
            wMonthExitWarehouseStat.setCreateBy("MxTask");
            wMonthExitWarehouseStat.setCreateTime(DateUtils.getNowDate());

            /**
             * 月出库量，插入数据
             */
            wMonthExitWarehouseStatService.insertWMonthExitWarehouseStat(wMonthExitWarehouseStat);
            //sleep(300);
            /**
             * 获取插入记录，ID
             */
            monthExitStatisticId = wMonthExitWarehouseStat.getMonthExitStatisticId();
            //logger.debug("OOO " + "monthExitStatisticId = " + monthExitStatisticId);

            /**
             * exitWarehouseWeightTotal清零
             */
            exitWarehouseWeightTotal = 0.0;
            exitWarehouseTheoryWeightTotal = 0.0;

            /**
             * 入出库量关联表，数据插入
             */
            //logger.debug("OOO " + "goodsIds = " + goodsIds);
            wMonthExitWarehouseGoodsRelService.insertWMonthExitWarehouseGoodsRelExt(monthExitStatisticId, goodsIds);
            goodsIds.clear();

        }

        logger.debug("统计，月出库量，出库客户，货物类型，结束 ...");
    }

}
